package com.sdlg.controller;
import com.sdlg.pojo.*;
import com.sdlg.service.LryService;
import com.sdlg.service.MemberService;
import com.sdlg.service.RootService;
import com.sdlg.util.PageResult;
import com.sdlg.util.QueryPageBean;
import com.sdlg.util.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.UUID;

@Controller
@RequestMapping("/membermanger")
public class MemberController {

    @Resource
    private MemberService memberService;

    @Resource
    private LryService lryService;

    @Resource
    private RootService rootService;

    @Value("${setmealpic.path}")
    private String setmealPath;

    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=memberService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/getMemberInfo")
    @ResponseBody
    public Result getMemberInfo(Integer id){
        Result result=memberService.getMemberInfo(id);
        return result;
    }
    @RequestMapping("/getMemberAllInfo")
    @ResponseBody
    public Result getMemberAllInfo(Integer id){
        Result result=memberService.getMemberAllInfo(id);
        return result;
    }

    @RequestMapping("/savePassword")
    @ResponseBody
    public Result savePassword(Integer id,@RequestBody Member member){
        Result result=memberService.savePassword(id,member);
        return result;
    }

    @RequestMapping("/deleteInfoById")
    public Result deleteInfoById(@RequestParam("id")Integer id){
        Result result=memberService.deleteInfoById(id);
        return result;
    }

    //登录方法
    @RequestMapping("/login")
    public String login(String username, String password, String role, HttpServletRequest request){
        if(role.equals("root")){
            Root root=null;
            try{
                root=rootService.login(username,password,request);
            }catch (Exception ex){
                ex.printStackTrace();
            }
            if(root!=null){
                return "redirect:/pages/main.html?id="+root.getId()+"&&role="+root.getRoleId();
            }else{
                return "redirect:/index.html";
            }
        }else if(role.equals("lry"))
        {
            Lry lry=null;
            try{
                lry=lryService.login(username,password,request);
            }catch (Exception ex){
                ex.printStackTrace();
            }
            if(lry!=null){

                return "redirect:/pages/main.html?id="+lry.getId()+"&&role="+lry.getRoleId();
            }else{
                return "redirect:/index.html";
            }
        }
        else if(role.equals("member"))
        {
            Member member=null;
            try{
                member=memberService.login(username,password,request);
            }catch (Exception ex){
                ex.printStackTrace();
            }

            if(member!=null){
                return "redirect:/pages/main.html?id="+member.getId()+"&&role="+member.getRoleId();
            }else{
                return "redirect:/index.html";
            }
        }
        return null;
    }

    @RequestMapping("/getUserAndRoleInfo")
    @ResponseBody
    public Result getUserAndRoleInfo(Integer id,Integer role){
        Result result=null;
        if(role==1){
            result=rootService.getUserAndRoleInfo(id);
        }else if(role==2){
            result=lryService.getUserAndRoleInfo(id);
        }else if(role==3){
            result=memberService.getUserAndRoleInfo(id);
        }
        return result;
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public Result saveUser(@RequestBody Member member){
        Result result = memberService.saveUser(member);
        return result;
    }

    @RequestMapping("/saveAllInfo")
    @ResponseBody
    public Result saveAllInfo(@RequestBody Member member){
        Result result = memberService.saveAllInfo(member);
        return  result;
    }

    @RequestMapping("/bookRent")
    @ResponseBody
    public Result bookRent(Integer id, String bookid,@RequestBody Order order){
        Integer bookids=Integer.parseInt(bookid);
        Result result=memberService.bookRent(id,bookids,order);
        return result;
    }

    @RequestMapping("/uploadFile")
    public Result uploadFile(MultipartFile imgFile){
        //存放图片的位置
        String path=setmealPath;
        //获取图片的原名称 2.jpg
        String strName=imgFile.getOriginalFilename();
        int lastIndexOf=strName.lastIndexOf('.');
        String str1=strName.substring(lastIndexOf);//.jpg
        String uuidStr= UUID.randomUUID().toString();//342dir32432jewr
        String newFileName=uuidStr+str1;//342dir32432jewr.jpg
        System.out.println("http://localhost:8088/setmealpic/"+newFileName);
        File file=new File(path+newFileName);
        try{
            imgFile.transferTo(file);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return new Result(true,null,newFileName);
    }



}
